package com.servers;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import com.po.Person;
import com.servers.threads.DatabaseQueryforUser;

public class ServersForDatabase {

	static boolean isopen = false;
	private static Map<String, Person> mapPerson = new HashMap<String, Person>();
	public static Map<String, Person> getMapPerson() {
		return mapPerson;
	}

	public static void setMapPerson(Map<String, Person> mapPerson) {
		ServersForDatabase.mapPerson = mapPerson;
	}

	private static ServerSocket serverSocket; 
	
	public static boolean isOpen() {
		return isopen;
	}
	
	public static void openServersForDatabase() throws IOException {
		serverSocket = new ServerSocket(8888);
		isopen = true;
		while (isopen) {
			Socket socket = serverSocket.accept();
			new Thread(new DatabaseQueryforUser(socket, mapPerson)).start();
		}
		serverSocket.close();
	}
	
	
	public static void closeServer() throws IOException {
		isopen = false;
		serverSocket.close();
	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		ServerSocket serverSocket = new ServerSocket(8888);
		String breakServer = "ss";
		while (!(breakServer.equals("exitServer"))) {
			Socket socket = serverSocket.accept();
			new Thread(new DatabaseQueryforUser(socket, mapPerson)).start();
		}
		serverSocket.close();
	}

}
